//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CSimpleGeometry9.cpp>
///	@path	~/src/graphics/dx9/
///	@date	2007/10/25
///	@desc	.

#include "config/config.h"

#include "graphics/dx9/CSimpleGeometry9.h"

namespace xeres {

	IMPL_IOBJECT_CLASS( CSimpleGeometry9 );

	// ctor
	CSimpleGeometry9::CSimpleGeometry9( size_t stride , size_t count , D3DPRIMITIVETYPE type , 
		RefWeak<IVertexLayout> layout , IDirect3DVertexBuffer9 * vertex , IDirect3DIndexBuffer9 * index )
		: m_stride( (ushort)stride )
		, m_count( count )
		, m_type( type )
		, m_layout( layout )
		, m_pVertexBuffer( vertex )
		, m_pIndexBuffer( index )
	{
	}

	// dtor
	CSimpleGeometry9::~CSimpleGeometry9( void )
	{
		if( m_pVertexBuffer )
		{
			UINT rest = m_pVertexBuffer->Release();
			if( rest != 0 )
			{
				TRACE_ERROR( _S("CSimpleGeometry9::DTOR: Invalid vertex buffer reference count.") );
				FATAL_ABORT( _S("Invalid IDirect3DVertexBuffer9 reference count.") );
			}
		}

		if( m_pIndexBuffer )
		{
			UINT rest = m_pIndexBuffer->Release();
			if( rest != 0 )
			{
				TRACE_ERROR( _S("CSimpleGeometry9::DTOR: Invalid index buffer reference count.") );
				FATAL_ABORT( _S("Invalid IDirect3DIndexBuffer9 reference count.") );
			}
		}
	}
	// GetLayout
	RefWeak<IVertexLayout> CSimpleGeometry9::GetLayout( void )
	{
		return m_layout;
	}
	// EstablishBuffer
	void CSimpleGeometry9::EstablishBuffer( RenderContext& rc )
	{
		m_layout->ApplyLayout( rc );
		rc.GetDX9()->SetStreamSource( 0 , m_pVertexBuffer, 0 , m_stride );
		if( m_pIndexBuffer )
		{
			rc.GetDX9()->SetIndices( m_pIndexBuffer );
		}
	}
	// draw geometry
	void CSimpleGeometry9::Draw( RenderContext& rc )
	{
		rc.GetDX9()->DrawPrimitive( (D3DPRIMITIVETYPE)m_type , 0 , (UINT)m_count );
	}
	// OnLostDevice
	bool CSimpleGeometry9::OnLostDevice( void )
	{
		return false;
	}
	// OnResetDevice
	bool CSimpleGeometry9::OnResetDevice( RefWeak<IDevice> device )
	{
		return false;
	}

} // namespace xeres
